/*
  CS 216 Final Project
 
  Copyright (C) 2012  Logan A. Wells, University of Kentucky

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
 
  This program is distributed with ABSOULTELY NO WARRANTY. See the
  GNU General Public License for more details. A copy of the license can
  be obtatined at <http://www.gnu.org/licenses/>.
*/


#include <iostream>
#include <sstream>
#include <cstdlib>
#include <getopt.h>

#include "race.h"
#include "racefactory.h"
#include "raceprocessor.h"
#include "trainer.h"
#include "trainerfactory.h"
#include "trainerprocessor.h"
#include "speed.h"
#include "speedfactory.h"
#include "endurance.h"
#include "endurancefactory.h"
#include "allaround.h"
#include "allaroundfactory.h"

class InputException{
public:
	InputException(const std::string& msg) : m_msg(msg){};
	const std::string& what() const {return m_msg;}
private:
	const std::string m_msg;
};// class InputException from sample code

int money_spent = 0;
int money_earned = 0;
int net_money = 0;

int main(int argc, char* argv[]) {
	
	//set up defualt values
	
	try {
		
		//initialize option values to default
		
		
		//process options
		const char* optstring = "abhmrs:t";
		const struct option long0pts[] = {
			{ "all",       no_argument,        0, 'a' },
			{ "best",      no_argument,        0, 'b' },
			{ "help",      no_argument,        0, 'h' },
			{ "money",     no_argument,        0, 'm' },
			{ "races",     no_argument,        0, 'r' },
			{ "stats",     required_argument,  0, 's' },
			{ "trainers",  no_argument,        0, 't' }};

		int c;
		int longIndex;
		while((c = getopt_long(argc, argv, optstring, long0pts, &longIndex)) != -1) {
			switch(c) {
				case 'a':
					//calculate all race combinations
					break;
				case 'b':
					//calculate best race combination
					break;
				case 'm':
					std::cout << " Money spent: " << money_spent << std::endl
							  << "Money earned: " << money_earned << std::endl
							  << "   Net Money: " << net_money << std::endl;
					break;
				case 'r':
					for(int i=0, raceprocessor::m_races.size(), ++i)
						std::cout << m_races[i] << std::endl;
					break;
				case 's':
					std::cout << "Your Horse's stats:" << std::endl;
					for(int i=0; i<12; ++i)
						std::cout << "  Month " << i+1 << " : " << horse.stats[i];
					break;
				case 't':
					for(int i=0, trainerprocessor::m_trainers.size(), ++i)
						std::cout << m_trainers[i] << std::endl;
					break;
				case '?':
				case 'h':
				default:
				{
					std::cerr << "Usage: " << argv[0] << "[-abmr -s # -t]\n";
					std::cerr << "Options:\n"
					<< "-a, --all         Print all race/trainer combinations with stats and money\n"
					<< "-b, --best        Print best combination with stats and money\n"
					<< "-h, --help        Shows this message\n"
					<< "-m, --money       Print money spent, earned and net total of best combination\n"
					<< "-r, --races       Print all race information\n"
					<< "-s, --stats       Print horse stats after each month with specified trainer\n"
					<< "-t, --trainers    Print all trainer applications\n";
					exit(-1);
				}
			}
		}
				
		
		// process non-option arguments
		if(argc-optind>0) {
			std::cout << "Found additional arguments:\n";
			for(int i=optind; i<argc; ++i)
				std::cout << "argv[" << i << "]: " << argv[i] << '\n';
			std::cout << std::endl;
		}

		
	}
	catch (InputException& e){
		std::cerr << "Invalid input: " << e.what() << '\n';
		std::cerr << "Use the --help option for usage information\n";
		exit(2);
	}
	catch(...){
		std::cerr << "Unknown exception caught\n";
		exit(3);
	}
	
	return 0;
}
